import { defineStore } from 'pinia';

// 假设这里可以获取到 commit root action 的方法，实际使用中需要根据情况调整
import { useAppStore } from './app'; 

export const useTagsViewStore = defineStore('tagsView', {
    state: () => ({
        visitedViews: [],
        cachedViews: [],
        currentLabel: null,
        tabsViews: []
    }),
    actions: {
        setCurrentLabel(value) {
            this.currentLabel = value;
        },
        addVisitedView(view) {
            if (this.visitedViews.some(v => v.path === view.path)) return;
            this.visitedViews.push(
                Object.assign({}, view, {
                    title: view.meta.title || 'no-name'
                })
            );
        },
        addTabsView(view) {
            if (this.tabsViews.some(v => v.key === view.key)) {
                this.tabsViews.forEach(item => {
                    if (item.key === view.key) {
                        item.activeName = view.activeName;
                    }
                });
            } else {
                this.tabsViews.push(view);
            }
        },
        addCachedView(view) {
            if (this.cachedViews.includes(view.name)) return;
            if (view.meta && !view.meta.noCache) {
                this.cachedViews.push(view.name);
            }
        },
        delVisitedView(view) {
            for (const [i, v] of this.visitedViews.entries()) {
                if (v.path === view.path) {
                    this.visitedViews.splice(i, 1);
                    break;
                }
            }
            return [...this.visitedViews];
        },
        delCachedView(view) {
            const appStore = useAppStore();
            appStore.clearViewCacheByKey(view);

            const index = this.cachedViews.indexOf(view.name);
            if (index > -1) {
                this.cachedViews.splice(index, 1);
            }
            return [...this.cachedViews];
        },
        delTabsView(view) {
            this.tabsViews = this.tabsViews.filter(item => item.path!== view.path);
        },
        delOthersVisitedViews(view) {
            this.visitedViews = this.visitedViews.filter(v => {
                return v.meta.affix || v.path === view.path;
            });
            return [...this.visitedViews];
        },
        delOthersCachedViews(view) {
            const appStore = useAppStore();
            appStore.clearViewCacheOther(view);

            const index = this.cachedViews.indexOf(view.name);
            if (index > -1) {
                this.cachedViews = this.cachedViews.slice(index, index + 1);
            } else {
                this.cachedViews = [];
            }
            return [...this.cachedViews];
        },
        delAllVisitedViews() {
            const affixTags = this.visitedViews.filter(tag => tag.meta.affix);
            this.visitedViews = affixTags;
            return [...this.visitedViews];
        },
        delAllCachedViews() {
            const appStore = useAppStore();
            appStore.clearViewCache();

            this.cachedViews = [];
            return [...this.cachedViews];
        },
        updateVisitedView(view) {
            for (let v of this.visitedViews) {
                if (v.path === view.path) {
                    Object.assign(v, view);
                    break;
                }
            }
        },
        delRightViews(view) {
            const index = this.visitedViews.findIndex(v => v.path === view.path);
            if (index === -1) {
                return;
            }
            this.visitedViews = this.visitedViews.filter((item, idx) => {
                if (idx <= index || (item.meta && item.meta.affix)) {
                    return true;
                }
                const i = this.cachedViews.indexOf(item.name);
                if (i > -1) {
                    this.cachedViews.splice(i, 1);
                }
                return false;
            });
            const appStore = useAppStore();
            appStore.saveViewCache(this.visitedViews);
            return [...this.visitedViews];
        },
        delLeftViews(view) {
            const index = this.visitedViews.findIndex(v => v.path === view.path);
            if (index === -1) {
                return;
            }
            this.visitedViews = this.visitedViews.filter((item, idx) => {
                if (idx >= index || (item.meta && item.meta.affix)) {
                    return true;
                }
                const i = this.cachedViews.indexOf(item.name);
                if (i > -1) {
                    this.cachedViews.splice(i, 1);
                }
                return false;
            });
            const appStore = useAppStore();
            appStore.saveViewCache(this.visitedViews);
            return [...this.visitedViews];
        },
        addView(view) {
            this.addVisitedView(view);
            this.addCachedView(view);
        },
        delView(view) {
            const appStore = useAppStore();
            appStore.clearViewCacheByKey(view);

            this.delVisitedView(view);
            this.delCachedView(view);
            return {
                visitedViews: [...this.visitedViews],
                cachedViews: [...this.cachedViews]
            };
        },
        delOthersViews(view) {
            this.delOthersVisitedViews(view);
            this.delOthersCachedViews(view);
            return {
                visitedViews: [...this.visitedViews],
                cachedViews: [...this.cachedViews]
            };
        },
        delAllViews(view) {
            const appStore = useAppStore();
            appStore.clearViewCache();

            this.delAllVisitedViews();
            this.delAllCachedViews();
            return {
                visitedViews: [...this.visitedViews],
                cachedViews: [...this.cachedViews]
            };
        },
        delRightTags(view) {
            return this.delRightViews(view);
        },
        delLeftTags(view) {
            return this.delLeftViews(view);
        }
    }
});